﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PesticideManage.API.Data;
using PesticideManage.API.Models.Sms;
using Shang.Sms;
using Shang.Sms.Models;

namespace PesticideManage.API.Controllers;
[Route("api/[controller]")]
[ApiController]
[AllowAnonymous]
public class SmsController : ControllerBase
{
    private readonly ISmsService _smsService;
    private readonly PesticideContext context;

    public SmsController(ISmsService smsService, PesticideContext context)
    {
        _smsService = smsService;
        this.context = context;
    }
    /// <summary>
    /// 验证短信验证码
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("[action]")]
    public IActionResult VerifyCode(VerifyCodeDTO dto)
    {
        if (_smsService.VerifyCode(dto.MobileNumber, dto.Smsvcode))
        {
            return NoContent();
        }
        return BadRequest("验证码错误");
    }
    /// <summary>
    /// 发送短信
    /// </summary>
    /// <param name="mobileNumber">手机号码</param>
    /// <returns>发送短信的状态</returns>
    [HttpPost("[action]")]
    public async Task<ActionResult<SendStatus>> SendAsync(string mobileNumber)
    {
        //if (!await context.Users.AnyAsync(u => u.MobileNumber == mobileNumber))
        //{
        //    return BadRequest("用户不存在");
        //}
        return await _smsService.SendSmsCodeAsync(mobileNumber);
    }
    /// <summary>
    /// 发送注册短信
    /// </summary>
    /// <param name="mobileNumber">手机号码</param>
    /// <returns>发送短信的状态</returns>
    [HttpPost("[action]")]
    public async Task<ActionResult<SendStatus>> SendRegistrationSMSVerificationCode(string mobileNumber)
    {

        if (await context.Users.AnyAsync(u => u.MobileNumber == mobileNumber))
        {
            return BadRequest("用户已存在");
        }
        return await _smsService.SendSmsCodeAsync(mobileNumber);
    }
    /// <summary>
    /// 发送登录短信
    /// </summary>
    /// <param name="mobileNumber">手机号码</param>
    /// <returns>发送短信的状态</returns>
    [HttpPost("[action]")]
    public async Task<ActionResult<SendStatus>> SendLoginSMSVerificationCode(string mobileNumber)
    {
        if (!await context.Users.AnyAsync(u => u.MobileNumber == mobileNumber))
        {
            return BadRequest("用户不存在");
        }
        return await _smsService.SendSmsCodeAsync(mobileNumber);
    }

}
// {1}（地道益农平台验证码，{2}分钟内有效） 1203621
// 您正在申请{1}，验证码为：{2}，{3}分钟内有效！ 1203643
